package org.dromara.canteen.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.school.domain.Campus;
import org.dromara.system.domain.SysOss;
import org.springframework.stereotype.Service;
import org.dromara.canteen.domain.bo.CanteenEmployeeBo;
import org.dromara.canteen.domain.vo.CanteenEmployeeVo;
import org.dromara.canteen.domain.CanteenEmployee;
import org.dromara.canteen.mapper.CanteenEmployeeMapper;
import org.dromara.canteen.service.ICanteenEmployeeService;
import com.github.yulichang.base.MPJBaseServiceImpl;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 从业人员管理Service业务层处理
 *
 * @author Gjb
 * @date 2025-07-28
 */
@RequiredArgsConstructor
@Service
public class CanteenEmployeeServiceImpl  extends MPJBaseServiceImpl<CanteenEmployeeMapper,CanteenEmployee> implements ICanteenEmployeeService {

    private final CanteenEmployeeMapper baseMapper;

    /**
     * 查询从业人员管理
     */
    @Override
    public CanteenEmployeeVo queryById(Long employeeId){
        return baseMapper.selectVoById(employeeId);
    }

    /**
     * 查询从业人员管理列表
     */
    @Override
    public TableDataInfo<CanteenEmployeeVo> queryPageList(CanteenEmployeeBo bo, PageQuery pageQuery) {
        TenantHelper.clearDynamic();
        MPJLambdaWrapper<CanteenEmployee> mpj= JoinWrappers.lambda(CanteenEmployee.class)
            .selectAll(CanteenEmployee.class)
            .selectAs(Campus::getCampusName, CanteenEmployeeVo::getSchoolCampusName)
            .leftJoin(Campus.class,Campus::getCampusId,CanteenEmployee::getSchoolCampusId)
            .selectAs(SysOss::getUrl,CanteenEmployeeVo::getOssUrl)
            .leftJoin(SysOss.class,SysOss::getOssId,CanteenEmployee::getHc)
            .logicDelToOn();
        if(bo.getSchoolCampusId()!=null){
            mpj.eq(CanteenEmployee::getSchoolCampusId,bo.getSchoolCampusId());
        }
//        LoginUser loginUser = LoginHelper.getLoginUser();
//        UserType userType = UserType.getUserType(loginUser.getUserType());
//        if(bo.getTenantId()!=null){
//            mpj.eq(Campus::getTenantId,bo.getTenantId());
//        }
//        switch (userType) {
//            case SYS_USER:
//                break;
//            case SCHOOLADMIN:
//                break;
//            case TEACHER:
//                break;
//            default:
//                return null;
//        }
        IPage<CanteenEmployeeVo> ipages=baseMapper.selectJoinPage(
            new Page<CanteenEmployeeVo>(pageQuery.getPageNum(), pageQuery.getPageSize()),
            CanteenEmployeeVo.class,mpj
        );
        TableDataInfo<CanteenEmployeeVo> result = TableDataInfo.build(ipages);
        return result;
    }

    /**
     * 查询从业人员管理列表
     */
    @Override
    public List<CanteenEmployeeVo> queryList(CanteenEmployeeBo bo) {
        LambdaQueryWrapper<CanteenEmployee> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CanteenEmployee> buildQueryWrapper(CanteenEmployeeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CanteenEmployee> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getEmployeeId() != null, CanteenEmployee::getEmployeeId, bo.getEmployeeId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), CanteenEmployee::getName, bo.getName());
        lqw.eq(bo.getSex() != null, CanteenEmployee::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getCardId()), CanteenEmployee::getCardId, bo.getCardId());
        lqw.eq(StringUtils.isNotBlank(bo.getPost()), CanteenEmployee::getPost, bo.getPost());
        lqw.eq(StringUtils.isNotBlank(bo.getHc()), CanteenEmployee::getHc, bo.getHc());
        lqw.eq(StringUtils.isNotBlank(bo.getHcNumber()), CanteenEmployee::getHcNumber, bo.getHcNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getHcValidity()), CanteenEmployee::getHcValidity, bo.getHcValidity());
        lqw.eq(StringUtils.isNotBlank(bo.getTcLevel()), CanteenEmployee::getTcLevel, bo.getTcLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getTcValidity()), CanteenEmployee::getTcValidity, bo.getTcValidity());
        lqw.eq(bo.getSchoolCampusId() != null, CanteenEmployee::getSchoolCampusId, bo.getSchoolCampusId());
        lqw.eq(StringUtils.isNotBlank(bo.getContact()), CanteenEmployee::getContact, bo.getContact());
        return lqw;
    }

    /**
     * 新增从业人员管理
     */
    @Override
    public Boolean insertByBo(CanteenEmployeeBo bo) {
        CanteenEmployee add = MapstructUtils.convert(bo, CanteenEmployee.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setEmployeeId(add.getEmployeeId());
        }
        return flag;
    }

    /**
     * 修改从业人员管理
     */
    @Override
    public Boolean updateByBo(CanteenEmployeeBo bo) {
        CanteenEmployee update = MapstructUtils.convert(bo, CanteenEmployee.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CanteenEmployee entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除从业人员管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
